package com.duqc.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @ClassName SelectorClient
 * @Description 选择器 异步非阻塞IO
 * @Author duqc
 * @Date 2021/8/23
 **/
public class SelectorClient {

    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 6666);
        socketChannel.configureBlocking(false);
        //连接服务器
        boolean connect = socketChannel.connect(address);
        if (!connect) {
            while (!socketChannel.finishConnect()) {
                System.out.println("连接服务器...");
            }
        }
        String msg = "hello java技术爱好者！";
        ByteBuffer wrap = ByteBuffer.wrap(msg.getBytes());
        socketChannel.write(wrap);
        System.in.read();
    }

}
